package draw_by_gocui

import (
	"strings"
	"gitee.com/lwow2025/snake-go/game"
)

func MakeAFrame(width int, height int, ps []*game.Point) ([]string) {
	pm := pointsToMap(ps)
	result := make([]string, height)
	var sb strings.Builder
	sb.Grow(width)

	for i := 0; i < height; i++ {
		sb.Reset()
		rowPs := pm[i]
		for j := 0; j < width; j++ {
			if rowPs != nil && rowPs[j] != nil {
				sb.WriteString(rowPs[j].GetFace())
			} else {
				sb.WriteByte(' ')
			}
		}
		result[i] = sb.String()
	}

	return result[:]
}

func pointsToMap(ps []*game.Point) map[int]map[int]*game.Point {
	result := map[int]map[int]*game.Point{}
	for _, p := range ps {
		if result[p.Y] == nil {
			result[p.Y] = map[int]*game.Point{}
		}
		result[p.Y][p.X] = p
	}
	return result
}
